package com.example.administrator.gplineview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;

/**
 * Created by chenj
 * Created on 2017/4/21
 */

public class MySurfaceView extends SurfaceView implements Callback, Runnable {

    private Paint mDefaultPaint;
    private Path mPath;
    private SurfaceHolder mHolder;
    private int mHeight;
    private int mWidth;
    private Canvas mCanvas;
    private int mCurrentWidth;
    private Runnable mAction;
    private Thread mThread;

    public MySurfaceView(Context context) {
        this(context, null);
    }

    public MySurfaceView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MySurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setKeepScreenOn(true);
        setFocusable(true);
        setFocusableInTouchMode(true);

        mHolder = getHolder();
        mHolder.addCallback(this);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mDefaultPaint = new Paint();
        mDefaultPaint.setAntiAlias(true);
        mDefaultPaint.setColor(Color.RED);
        mDefaultPaint.setTextSize(DensityUtils.sp2px(getContext(), 10));
        mDefaultPaint.setStyle(Paint.Style.STROKE);
        mHeight = getMeasuredHeight();
        mWidth = getMeasuredWidth();
        mPath = new Path();
        mThread = new Thread(this);
        mThread.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
//        mThread.destroy();
        mThread = null;
    }

    @Override
    public void run() {
        mAction = new Runnable() {
            @Override
            public void run() {
                if (mCurrentWidth == 0) {
                    mPath.moveTo(0, mHeight / 2);
                } else {
                    mPath.lineTo(mCurrentWidth, mHeight / 2 - (float) (Math.sin(mCurrentWidth * 1.0 / mWidth * 10) * mHeight / 4));
                }
                mCurrentWidth += 20;
                mCanvas = mHolder.lockCanvas();
                if (mCurrentWidth <= mWidth) {
                    if (mCanvas != null)
                        mCanvas.drawPath(mPath, mDefaultPaint);
                } else {
                    mCurrentWidth = 0;
                    mCanvas.drawColor(Color.BLACK, PorterDuff.Mode.CLEAR);
                }
                if (mCanvas != null)
                    mHolder.unlockCanvasAndPost(mCanvas);
                postDelayed(this, 20);
            }
        };
        postDelayed(mAction, 20);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        removeCallbacks(mAction);
        getHandler().removeCallbacksAndMessages(null);
    }
}
